//app.js
let that;
App({
  cfg: {
    apiCount: 1,
    debug: true,
  },
  onLaunch: function(opt) {
    console.log("-----" + JSON.stringify(opt))
    let scene = decodeURIComponent(opt.scene);
    console.log(scene)
    if (scene == '1047') {//1047指小程序扫码场景值   这个opt。scene取值应该不对
      wx.setStorageSync('sceneID', opt.query.scene)
      console.log(wx.getStorageSync('sceneID'))
    }
    that = this;
    var openId = (wx.getStorageSync('openId'))
    if (!openId) {
      wx.login({
        success: function(res) {
          console.log(res.code)
          if (res.code) {
            that.getData({
              //后台接口地址
              url: that.api.getUserInfo(res.code),
              data: '',
              ok: function(res) {
                if (res.statusCode == 200 && res.errMsg == 'request:ok') {
                  if (res.data.code == 1) {
                    that.globalData.loginData = JSON.parse(res.data.data)
                    console.log(that.globalData.loginData)
                    wx.setStorageSync('openId', that.globalData.loginData.openid);
                    wx.setStorageSync('session_key', that.globalData.loginData.session_key);
                  } else {
                    wx.showToast({
                      title: res.data.message,
                      icon:'none'
                    })
                  }
                } else {
                  wx.showToast({
                    title: res.data.message,
                    icon: 'none'
                  })
                }
              }
            })
          }
        },
        fail: function(res) {
          console.log(res)
        }
      })
    }

  },
  
  globalData: {
    userInfo: null,
    loginData: {
      openid: null,
      session_key: null
    },
    locationInfo: null,
    shopData: null,
    companyId: '',
    memberLevelId: ''
  },
  api: require('./urlConfig/requestUrl.js').requestUrl,
  /**
   * 请求接口方法封装
   * 使用方法如下：在应运的js中page外面获取 let app= getApp()然后在需要的地方如下写
   *  app.getData({
      url: "",
      method: "POST",
      data: "",
      ok: res => {
        console.log("123")
      }
    })
   * url（接口地址连接） method（方法）data（传递参数）
   * ok中返回成功/失败/完成状态
   */
  getData(opt) {
    //console.trace()
    let count = this.cfg.apiCount;
    this.cfg.apiCount++;
    let par = {
      url: opt.url,
      method: '',
      data: {},
    };

    if (par.url) {

    } else {
      console.error('缺少关键参数:url')
    }
    par.method = opt.method || 'GET';

    if (opt.data) {
      par.data = opt.data || ''
    }
    if (opt.dataType) {
      par.dataType = opt.dataType;
    }
    if (this.cfg.debug) {
      console.log('%c◤ 【' + count + '】 @url: ' + par.url + ',  @param:', this.cfg.ajaxStartCss);
      console.log(par.data);
    }
    if (!par.openid) {
      par.openid = this.globalData.openid;
    }

    par.timestamp = new Date().getTime();
    wx.request({
      url: par.url,
      data: par.data,
      method: par.method,
      header: {
        Authentication: par.openid,
        timestamp: par.timestamp
      },
      complete: res => {
        if (this.cfg.debug) {
          console.log(res);
          console.log('%c◣ 【' + count + '】 @url: ' + par.url + '  ~data:', this.cfg.ajaxStartCss);
        }
        if (opt.ok && typeof opt.ok == 'function') {
          opt.ok(res)
        }
      }
    })
  },
  isLogin: function() {
    var a = wx.getStorageSync('vipLevelInfo');
    if (a) {
      return true
    } else {
      return wx.showModal({
        title: '提示',
        content: '您未注册会员，请先开通会员卡！',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/page/index/pages/cardOpen'
            })
          }
        },
        fail: function(res) {

        }
      })
    }
  }
})